//
//  YDBaseMessageViewController.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/1/18.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDBaseMessageViewController.h"
#import "YDMessageViewController.h"
#import "YDNotiViewController.h"

@interface YDBaseMessageViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)UIScrollView *scrollView; //滚动视图；

/**
 分段控制
 */
@property (nonatomic, strong)UISegmentedControl *segmentControl;
@end

@implementation YDBaseMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:mainGray];
    [self.navigationController.navigationBar.topItem setTitleView:self.segmentControl];
    
    [self addChildViewController:[YDNotiViewController new]];
    [self addChildViewController:[YDMessageViewController new]];
    
    
    [self.view addSubview:self.scrollView];
    [self addChildViewFromIndex:self.type];
    if (self.type == PZMessage) {
        [self.scrollView setContentOffset:CGPointMake(LKScreenW, 0) animated:YES];
    }
    
    
}


- (void)segmentDidSelected:(UISegmentedControl *)segment
{
    
    if (!self.childViewControllers.count) {return;}
    
    NSInteger currentIndex =  segment.selectedSegmentIndex;
    
    
    [self.scrollView setContentOffset:CGPointMake(LKScreenW *currentIndex, 0) animated:YES];
    
    [self addChildViewFromIndex:currentIndex];
    
}



#pragma mark - UIScrollviewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView) {
        CGFloat offsetx = scrollView.contentOffset.x / LKScreenW;
        if (offsetx >= 0.5) {
            [self.segmentControl setSelectedSegmentIndex:1];
        }else
        {
            [self.segmentControl setSelectedSegmentIndex:0];
        }
        
        [self addChildViewFromIndex:@(offsetx).integerValue];
        
    }
    
    
    
}


/**
 添加自控制的view到scrollview
 */
- (void)addChildViewFromIndex:(NSInteger)index
{
    UIViewController *targetvc = [self.childViewControllers objectAtIndex:index];
    
    // 如果已经加载过，就不再加载
    if ([targetvc isViewLoaded]) return;
    
    targetvc.view.frame = CGRectMake(LKScreenW*index, 0, LKScreenW,self.scrollView.height);
    [self.scrollView addSubview:targetvc.view];
}


#pragma mark - getter
- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        CGFloat safeTopH = isPhoneX ? 84 : 64;
        CGFloat safeBottomH = isPhoneX ? 34 : 0;
        _scrollView.frame = CGRectMake(0, 0, LKScreenW, LKScreenH - safeTopH - safeBottomH);
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(LKScreenW *2, 0);
        _scrollView.pagingEnabled = YES;
    }
    
    return _scrollView;
}
-(UISegmentedControl *)segmentControl
{
    if (_segmentControl == nil) {
        NSArray *titleArr = @[@"通知",@"消息"];
        
        UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:titleArr];
        
        segmentControl.frame =CGRectMake(0,0,rate375(160), 26);
        [segmentControl setSelectedSegmentIndex:0];
        segmentControl.tintColor = [UIColor whiteColor];
        [segmentControl addTarget:self action:@selector(segmentDidSelected:) forControlEvents:UIControlEventValueChanged];
        [segmentControl setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithWhite:1 alpha:0.6],
                                                 NSFontAttributeName:[UIFont systemFontOfSize:16]
                                                 }
                                      forState:UIControlStateNormal];
        
        [segmentControl setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"0E4483"],
                                                 NSFontAttributeName:[UIFont systemFontOfSize:16]}
                                      forState:UIControlStateSelected];
        
        _segmentControl = segmentControl;
    }
    return _segmentControl;
}

@end
